function getLength(inputString, utfLen){
    if (!inputString) 
        return 0;
    var length = 0;
    var utf_len = utfLen ? utfLen : 2;
    var enc_utf8 = escape(inputString);
    var is_percent = false;
    // calculate the length of string
    for (var i = 0; i < enc_utf8.length; i++) {
        var curr_char = enc_utf8.charAt(i).toLowerCase();
        if (is_percent) { // pervious char is '%'
            if (curr_char == 'u') {
                length += utf_len;
                i += 4;
            }
            else {
                length++;
                i += 1;
            }
            is_percent = false;// turnoff the flag
            continue;
        }
        if (curr_char == '%') {
            is_percent = true;
            continue;
        }
        length++;
    }
    return length;
};
function renderForMul(indexname){
    return function(v, params, record, rowIndex, colIndex, store){
        return record.data[indexname];
    }
};

function renderForMulData(indexname, data_format){
    return function(v, params, record, rowIndex, colIndex, store){
        return (record.data[indexname]).dateFormat(data_format);
    }
};

function ComboRender(combox1){
    return function(value){
        var text = value;
        
        if (combox1.valueField) {
            var r = combox1.findRecord(combox1.valueField, value);
            
            if (r) {
                text = r.data[combox1.displayField];
            }
            else 
                if (combox1.valueNotFoundText !== undefined) {
                    text = combox1.valueNotFoundText;
                }
        }
        return text;
        
    }
};


function ComboRender4refresh(combox1, grid){
    return function(value){
        var text = value;
        
        if (combox1.valueField) {
            var r = combox1.findRecord(combox1.valueField, value);
            
            if (r) {
                text = r.data[combox1.displayField];
            }
            else {
            
                if (!combox1.store.gridrefresh) {
                    combox1.store.gridrefresh = 0;
                };
                if (combox1.store.gridrefresh == 0) {
                    var t = function(){
                        grid.getView().refresh();
                        
                        var x = function(){
                            combox1.store.gridrefresh = 0;
                        };
                        x.defer(1000);
                    }
.createDelegate(this)
                    
                    combox1.store.on('load', t);
                    combox1.store.gridrefresh = 1;
                    combox1.store.reload();
                }
                
            }
            
        }
        return text;
        
    }
};


function ComboRenderforlimit(combox1, col1, combovaluefield1, col2, combovaluefield2){
    return function(value, metadata, record){
        var text = value;
        
        
        if (combox1.valueField) {
            var r = combox1.findRecord(combox1.valueField, value);
            
            if (r) {
                text = r.data[combox1.displayField];
                record.data[col1] = r.data[combovaluefield1];
                record.data[col2] = r.data[combovaluefield2];
                
            }
            else 
                if (combox1.valueNotFoundText !== undefined) {
                    text = combox1.valueNotFoundText;
                }
        }
        
        return text;
        
    }
};

function ComboRenderforlimitSellstyle(combox1, col1, combovaluefield1, col2, combovaluefield2, col3, combovaluefield3){
    return function(value, metadata, record){
        var text = value;
        
        
        if (combox1.valueField) {
            var r = combox1.findRecord(combox1.valueField, value);
            
            if (r) {
                text = r.data[combox1.displayField];
                record.data[col1] = r.data[combovaluefield1];
                record.data[col2] = r.data[combovaluefield2];
                record.data[col3] = r.data[combovaluefield3];
                
            }
            else 
                if (combox1.valueNotFoundText !== undefined) {
                    text = combox1.valueNotFoundText;
                }
        }
        
        return text;
        
    }
};
function ComboRenderfor4(combox1, col1, combovaluefield1, col2, combovaluefield2, col3, combovaluefield3, col4, combovaluefield4){
    return function(value, metadata, record){
        var text = value;
        
        
        if (combox1.valueField) {
            var r = combox1.findRecord(combox1.valueField, value);
            
            if (r) {
                text = r.data[combox1.displayField];
                record.data[col1] = r.data[combovaluefield1];
                record.data[col2] = r.data[combovaluefield2];
                record.data[col3] = r.data[combovaluefield3];
                record.data[col4] = r.data[combovaluefield4];
                
            }
            else 
                if (combox1.valueNotFoundText !== undefined) {
                    text = combox1.valueNotFoundText;
                }
        }
        
        return text;
        
    }
};
function formatMoney(v){
    v = (Math.round((v - 0))) / 100;
    v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
    v = String(v);
    var ps = v.split('.');
    var whole = ps[0];
    var sub = ps[1] ? '.' + ps[1] : '.00';
    var r = /(\d+)(\d{3})/;
    while (r.test(whole)) {
        whole = whole.replace(r, '$1' + ',' + '$2');
    }
    v = whole + sub;
    return v;
};

function treeFormatMoney(v){
    var stripRe = /[\$,%]/g;
    v = (v !== undefined && v !== null && v !== '') ? parseFloat(parseInt(String(v).replace(stripRe, ""), 10) / 100, 10) : '';
    v = (Math.round((v - 0) * 100)) / 100;
    v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
    v = String(v);
    var ps = v.split('.');
    var whole = ps[0];
    var sub = ps[1] ? '.' + ps[1] : '.00';
    var r = /(\d+)(\d{3})/;
    while (r.test(whole)) {
        whole = whole.replace(r, '$1' + ',' + '$2');
    }
    v = whole + sub;
    return v;
};

function formatDate(value){
    return value ? Ext.Date.format(value, 'Y-m-d') : '';
};

function formatTime(value){
    return value ? Ext.Date.format(value, 'Y-m-d H:i:s') : '';
};

function TimeRender(value){
    return value ? value.substring(0, 4) + '-' + value.substring(4, 6) + '-' + value.substring(6, 8) + ' ' + value.substring(8, 10) + ':' + value.substring(10, 12) + ':' + value.substring(12, 14) : '';
};
function Convertmoney(v){
    var stripRe = /[\$,%]/g;
    return v !== undefined && v !== null && v !== '' ? parseFloat((parseInt(String(v).replace(stripRe, ""), 10) / 100).toFixed(2), 10) : '';
};

function unConvertmoney(v){
    var stripRe = /[\$,%]/g;
    return v !== undefined && v !== null && v !== '' ? parseInt((parseFloat(String(v).replace(stripRe, ""), 10) * 100).toFixed(2), 10) : '';
    
};
function unConvertdate(value){
    return value.dateFormat('Ymd');
};
function TypeUnconvert(record, recorditem){
    for (var i = 0; i < recorditem.length; i++) 
        if (recorditem[i].unconvert) {
            var oldvaule = record.data[recorditem[i].name];
            
            record.set(recorditem[i].name, recorditem[i].unconvert(oldvaule));
        }
    return record
};

function stateRender(val){
    if (val == 1) {
        return '<span style="color:green;">' + val + '</span>';
    }
    else 
        if (val == 2) {
            return '<span style="color:red;">' + val + '</span>';
        }
        else 
            if (val == 3) {
                return '<span style="color:orange;">' + val + '</span>';
            }
    // return val;
    return val;
}
